import { NextRequest } from "next/server";

const DATA = ["Tom", "Jerry"].map((name:string, index:number) => ({
  id: index,
  name,
}))

export async function GET(req: NextRequest) {
  console.log(req.nextUrl.searchParams)
  console.log(req.nextUrl.searchParams.get('name'))

  return Response.json({...DATA})
}

export async function POST(req: Request) {
  const payload = await req.json();
  const data = {
    id: DATA.length,
    name: payload?.name || ''
  }
  DATA.push(data)
  return Response.json(data)
}

export async function PUT(req: Request) {
  const payload = await req.json();
  const data:any = DATA.find((x:any) => x.id === (DATA.length - 1));
  if (!data) return new Response('')

  data.name = payload?.name || ''
  return Response.json(data)
}

export async function DELETE() {
  const data = DATA.pop();
  return Response.json(data)
}
